﻿//************************************************************ 
//Original Code by 
//Animating Shell Tray Icon with - C# By sudhir_Kumar
//http://www.codeproject.com/KB/cs/shelltray.aspx
//************************************************************ 
//Modify by Fernando Cerqueira - fenandocerqueira@msn.com
//SetIconRange: for context Type
//************************************************************ 

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
namespace Taskbar
{

    public class SystemTray
    {
        private System.Windows.Forms.NotifyIcon SysTrayIcon;
        private System.Windows.Forms.Timer m_AnimTimer;
        private int nTimeOut = 0;
        private bool bIsTimeOut = false;
        private Icon[] AnimIconList;
        private bool bAnimate = false;
        private int IconListPos = 0;
        private int _IntervalAnimation = 0;

        public delegate void TrayEventHandler(object sender, EventArgs e);
        public delegate void TrayMouseEventHandler(object sender, MouseEventArgs e);

        public event TrayEventHandler DoubleClick;
        public event TrayEventHandler Click;
        public event TrayEventHandler BalloonTipClicked;
        public event TrayEventHandler BalloonTipClosed;
        public event TrayEventHandler BalloonTipShown;
        public event TrayMouseEventHandler MouseUp;
        public event TrayMouseEventHandler MouseMove;
        public event TrayMouseEventHandler MouseDown;
        public event TrayMouseEventHandler MouseDoubleClick;
        public event TrayMouseEventHandler MouseClick;


        public SystemTray()
        {
            InitDefaults();
        }
        public SystemTray(string pTrayText)
        {
            InitDefaults();
            SysTrayIcon.Text = pTrayText;
        }

        public SystemTray(string pTrayText, Icon pIcon)
        {
            InitDefaults();
            SysTrayIcon.Text = pTrayText;
            SysTrayIcon.Icon = pIcon;
        }

        public SystemTray(string pTrayText, Icon pIcon, bool pAnimate)
        {
            InitDefaults();
            SysTrayIcon.Text = pTrayText;
            SysTrayIcon.Icon = pIcon;
            bAnimate = pAnimate;
        }

        private void InitDefaults()
        {
            this.SysTrayIcon = new System.Windows.Forms.NotifyIcon();
            this.m_AnimTimer = new System.Windows.Forms.Timer();
            this.m_AnimTimer.Interval = IntervalAnimation;
            //Set Handler
            m_AnimTimer.Tick += new System.EventHandler(this.SysTrayAnimator);
            this.SysTrayIcon.Click += new EventHandler(SysTrayIcon_Click);
            this.SysTrayIcon.DoubleClick += new EventHandler(SysTrayIcon_DoubleClick);
            this.SysTrayIcon.MouseClick += new MouseEventHandler(SysTrayIcon_MouseClick);
            this.SysTrayIcon.MouseDoubleClick += new MouseEventHandler(SysTrayIcon_MouseDoubleClick);
            this.SysTrayIcon.MouseDown += new MouseEventHandler(SysTrayIcon_MouseDown);
            this.SysTrayIcon.MouseMove += new MouseEventHandler(SysTrayIcon_MouseMove);
            this.SysTrayIcon.MouseUp += new MouseEventHandler(SysTrayIcon_MouseUp);
            this.SysTrayIcon.BalloonTipClicked += new EventHandler(SysTrayIcon_BalloonTipClicked);
            this.SysTrayIcon.BalloonTipClosed += new EventHandler(SysTrayIcon_BalloonTipClosed);
            this.SysTrayIcon.BalloonTipShown += new EventHandler(SysTrayIcon_BalloonTipShown);
            this.SysTrayIcon.Visible = true;
            bAnimate = false;
        }

        public void Dispose()
        {
            this.SysTrayIcon.Dispose();
            this.m_AnimTimer.Dispose();
        }


        public Icon Icon
        {
            get { return this.SysTrayIcon.Icon; }
            set
            {
                this.SysTrayIcon.Icon = value;
            }
        }

        public bool Visible
        {
            get
            {
                return this.SysTrayIcon.Visible;
            }
            set
            {
                this.SysTrayIcon.Visible = value;
            }
        }
        public string TrayText
        {
            get
            {
                return this.SysTrayIcon.Text;
            }
            set
            {
                this.SysTrayIcon.Text = value;
            }
        }

        public bool Animate
        {
            get
            {
                return bAnimate;
            }
            set
            {
                bAnimate = value;
            }
        }

        public string BaloonTipText
        {
            get
            {
                return SysTrayIcon.BalloonTipText;
            }
            set
            {
                SysTrayIcon.BalloonTipText = value;
            }
        }

        public string BalloonTipTitle
        {
            get
            {
                return SysTrayIcon.BalloonTipTitle;
            }
            set
            {
                SysTrayIcon.BalloonTipTitle = value;
            }
        }

        public ToolTipIcon BalloonTipIcon
        {
            get
            {
                return SysTrayIcon.BalloonTipIcon;
            }
            set
            {
                SysTrayIcon.BalloonTipIcon = value;
            }
        }

        public void SetContextMenuOrStrip(object value)
        {
            if (value.GetType().Name == "ContextMenu")
            {
                SysTrayIcon.ContextMenu = (ContextMenu)value;
            }

            if (value.GetType().Name == "ContextMenuStrip")
            {
                SysTrayIcon.ContextMenuStrip = (ContextMenuStrip)value;
            }
        }

        public ContextMenu ContextMenu
        {
            get
            {
                return SysTrayIcon.ContextMenu;
            }
            set
            {
                SysTrayIcon.ContextMenu = value;
            }
        }

        public int IntervalAnimation
        {
            get { if (_IntervalAnimation < 100) _IntervalAnimation = 100; return _IntervalAnimation; }
            set { if (value < 100) value = 100; _IntervalAnimation = value; this.m_AnimTimer.Interval = _IntervalAnimation; }
        }
        public ContextMenuStrip ContextMenuStrip
        {
            get
            {
                return SysTrayIcon.ContextMenuStrip;
            }
            set
            {
                SysTrayIcon.ContextMenuStrip = value;
            }
        }
        public void StartAnimation()
        {
            if (bAnimate)
                m_AnimTimer.Start();
        }
        public void StopAnimation()
        {
            if (bAnimate)
                m_AnimTimer.Stop();
        }

        public void StartAnimation(int timeOut)
        {
            if (bAnimate)
            {
                bIsTimeOut = true;
                nTimeOut = timeOut;
                m_AnimTimer.Start();
            }
        }
        public Icon[] GetAnimationIconList
        {
            get { return AnimIconList; }
        }

        public void ShowBaloonTrayTip(int nTimeout)
        {
            SysTrayIcon.ShowBalloonTip(nTimeout);
        }
        public void ShowBaloonTrayTip(int nTimeOut, string tipTitle, string tipText, ToolTipIcon tipIcon)
        {
            SysTrayIcon.ShowBalloonTip(nTimeOut, tipTitle, tipText, tipIcon);
        }

        public void SetIconRange(Type TypeParent, object[] IconList)
        {
            System.Type tp = IconList[0].GetType();
            if (tp.Name == "String")
            {
                AnimIconList = new Icon[IconList.Length];
                for (int i = 0; i < IconList.Length; ++i)
                {
                    AnimIconList[i] = new Icon(TypeParent, IconList[i].ToString());
                }

            }
            if (tp.Name == "Icon")
            {
                AnimIconList = new Icon[IconList.Length];
                for (int i = 0; i < IconList.Length; ++i)
                {
                    AnimIconList[i] = (Icon)IconList[i];
                }
            }
            if (IconList.Length > 0)
                Icon = AnimIconList[0];
        }
        private void SysTrayAnimator(object sender, System.EventArgs e)
        {
            m_AnimTimer.Stop();
            if (bIsTimeOut)
            {
                SysTrayIcon.Icon = AnimIconList[0];
                bIsTimeOut = false;
            }
            else
            {
                if (IconListPos > AnimIconList.Length)
                {
                    IconListPos = 0;
                }
                SysTrayIcon.Icon = AnimIconList[IconListPos];
                IconListPos++;
                if (IconListPos > AnimIconList.Length-1)
                {
                    IconListPos = 0;
                }
                m_AnimTimer.Start();
            }
        }

        void SysTrayIcon_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseMove != null)
                MouseMove(sender, e);
        }

        void SysTrayIcon_MouseDown(object sender, MouseEventArgs e)
        {
            if (MouseDown != null)
                MouseDown(sender, e);
        }

        void SysTrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (MouseDoubleClick != null)
                MouseDoubleClick(sender, e);
        }

        void SysTrayIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (MouseClick != null)
                MouseClick(sender, e);
        }

        void SysTrayIcon_Click(object sender, EventArgs e)
        {
            if (Click != null)
                Click(sender, e);
        }

        void SysTrayIcon_MouseUp(object sender, MouseEventArgs e)
        {
            if (MouseUp != null)
                MouseUp(sender, e);
        }

        void SysTrayIcon_DoubleClick(object sender, EventArgs e)
        {
            if (DoubleClick != null)
                DoubleClick(sender, e);
        }

        void SysTrayIcon_BalloonTipShown(object sender, EventArgs e)
        {
            if (BalloonTipShown != null)
                BalloonTipShown(sender, e);
        }

        void SysTrayIcon_BalloonTipClosed(object sender, EventArgs e)
        {
            if (BalloonTipClosed != null)
                BalloonTipClosed(sender, e);
        }

        void SysTrayIcon_BalloonTipClicked(object sender, EventArgs e)
        {
            if (BalloonTipClicked != null)
                BalloonTipClicked(sender, e);
        }
    }
}
